<?php
namespace app\api_wp_v01\controller;

class Index extends ApiWechatProgram {
	
	public function __construct() {
		
		// 载入配置文件
		\think\Config::load(CONF_PATH . 'api_wp' . DS . 'config.php');
	}
	
	/**
	 * 用户登录
	 */
	public function subscriber_login() {
		
		// 校验传入参数
		$parameter = \think\Request::instance()->post();
		$judge_1 = array_key_exists('code', $parameter) && is_string($parameter['code']) && (0 < mb_strlen($parameter['code'], 'utf8'));
		if(!$judge_1){
			return $this->disposeJson(1, '参数校验失败');
		};
		
		// 获取用户信息
		$query = array(
			'appid' => \think\Config::get('wp_service.app_id'),
			'secret' => \think\Config::get('wp_service.app_secret'),
			'js_code' => $parameter['code'],
			'grant_type' => 'authorization_code',
		);
		$target_url = \think\Config::get('wp_api.sns_jscode2session').'?'.http_build_query($query);
		$response = $this->sendRequest($target_url);
		if(!is_array($response)){
			return $this->disposeJson(9, $response);
		};
		
		// 判断获取结果
		if(array_key_exists('errcode', $response)){
			return $this->disposeJson(1, '获取用户信息失败，Code不正确');
		}else if(!array_key_exists('openid', $response)){
			return $this->disposeJson(1, '获取用户信息失败，未匹配到OpenId');
		};
		
		// 检查用户是否存在
		$time = time();
		$SubscriberModel = \app\common\model\SubscriberModel::where('wechat_id', $response['openid'])->find();
		if(null === $SubscriberModel){
			$SubscriberModel = new \app\common\model\SubscriberModel();
			$SubscriberModel->wechat_id = $response['openid'];
			$SubscriberModel->create_time = $time;
			$result = $SubscriberModel->save();
			if(false == $result){
				return $this->disposeJson(1, '保存用户数据失败');
			};
		};
		
		// 签发客户端令牌
		$login_digit = strval($time);
		$token_content = $SubscriberModel->getData('id') . '.' . $login_digit;
		$token_content = openssl_encrypt($token_content, \think\Config::get('client_token.encrypt_type'), \think\Config::get('client_token.encrypt_key'), \think\Config::get('client_token.encrypt_option'), \think\Config::get('client_token.encrypt_iv'));
		if(false === $token_content){
			return $this->disposeJson(1, '格式化用户标识符失败');
		};
		$application_name = \think\Config::get('application_name');
		$client_token = ucfirst($application_name) . '.ClientToken.' . $token_content;
		
		// 返回结果
		return $this->disposeJson(0, '', array(
			'client_token' => $client_token,
		));
		
	}
	
	// public function aaa() {
		
		// $url = 'https://apis.map.qq.com/ws/district/v1/list?key=V6EBZ-KYWK4-QMMUD-D4NT4-LNK2T-FFBIW';
		// $result = $this->sendRequest($url);
		
		// $data_array = array();
		// $RegionModel = new \app\common\model\RegionModel();
		// foreach($result['result'][0] as $value){
			// $data_array[] = array(
				// 'parent_id' => 0,
				// 'name' => $value['fullname'],
				// 'name_full' => $value['fullname'],
				// 'grade' => 1,
				// 'outside_id' => $value['id'],
			// );
		// };
		
		// $result = $RegionModel->saveAll($data_array);
		
		// return array(count($data_array), count($result));
	// }
	
	// public function bbb() {
		
		// $data_select = \app\common\model\RegionModel::where('name', 'in', array('北京市', '上海市', '天津市', '重庆市'))->select();
		
		// $data_array = array();
		// $RegionModel = new \app\common\model\RegionModel();
		// foreach($data_select as $value){
			// $data_array[] = array(
				// 'parent_id' => $value->id,
				// 'name' => $value->name,
				// 'name_full' => $value->name,
				// 'grade' => 2,
				// 'outside_id' => $value->outside_id,
			// );
		// };
		
		// $result = $RegionModel->saveAll($data_array);
		
		// return array(count($data_array), count($result));
		
	// }
	
	// public function ccc() {
		
		// set_time_limit(0);
		
		// $data_select = \app\common\model\RegionModel::where('grade', 1)->where('name', 'not in', array('北京市', '上海市', '天津市', '重庆市'))->select();
		
		// $data_array = array();
		// foreach($data_select as $key => $RegionModel){
			
			// $url = 'https://apis.map.qq.com/ws/district/v1/getchildren?key=V6EBZ-KYWK4-QMMUD-D4NT4-LNK2T-FFBIW&id=' . $RegionModel->outside_id;
			// $result = $this->sendRequest($url);
			
			// foreach($result['result'][0] as $value){
				// $data_array[] = array(
					// 'parent_id' => $RegionModel->id,
					// 'name' => $value['fullname'],
					// 'name_full' => $RegionModel->name . $value['fullname'],
					// 'grade' => 2,
					// 'outside_id' => intval($value['id']),
				// );
			// };
			
			// usleep(300000);
			
		// };
		
		// $RegionModel = new \app\common\model\RegionModel();
		// $result = $RegionModel->saveAll($data_array);
		
		// return array(count($result), count($data_array));
		
	// }
	
	// public function ddd() {
		
		// set_time_limit(0);
		
		// $data_select = \app\common\model\RegionModel::where('grade', 2)->select();
		
		// $data_array = array();
		// foreach($data_select as $key => $RegionModel){
			
			// $url = 'https://apis.map.qq.com/ws/district/v1/getchildren?key=V6EBZ-KYWK4-QMMUD-D4NT4-LNK2T-FFBIW&id=' . $RegionModel->outside_id;
			// $result = $this->sendRequest($url);
			
			// foreach($result['result'][0] as $value){
				// $data_array[] = array(
					// 'parent_id' => $RegionModel->id,
					// 'name' => $value['fullname'],
					// 'name_full' => $RegionModel->name_full . $value['fullname'],
					// 'grade' => 3,
					// 'outside_id' => intval($value['id']),
				// );
			// };
			
			// usleep(300000);
			
		// };
		
		// $RegionModel = new \app\common\model\RegionModel();
		// $result = $RegionModel->saveAll($data_array);
		
		// return array(count($result), count($data_array));
	// }
	
}